package com.three.crm.util;

import android.content.Context;
import android.content.Intent;

import com.three.crm.MyApplication;
import com.three.crm.constant.AppConstant;
import com.three.crm.constant.LoginVariable;
import com.three.crm.page.user.LoginActivity;


/**
 *Created by lqd on 2022-06-15.
 */
public class AppManager {
    private AppManager() {
        throw new UnsupportedOperationException("can't instance…");
    }

    public static long getTimesTamp(){
        return System.currentTimeMillis();
    }

    public static void setLoginAccount(String text){
        set(LoginVariable.LOGIN_ACCOUNT,text);
    }

    public static String getLoginAccount(){
        return (String) SharedPreferencesUtils.getInstances(MyApplication.getContext()).getSharedPreference(LoginVariable.LOGIN_ACCOUNT,"");
    }

    public static void setUserId(int text){
        set(LoginVariable.USERID,text);
    }

    public static int getUserId(){
        return (int) SharedPreferencesUtils.getInstances(MyApplication.getContext()).getSharedPreference(LoginVariable.USERID,0);
    }

    public static void setLevel(int text){
        set(LoginVariable.LEVEL,text);
    }

    public static int getLevel(){
        return (int) SharedPreferencesUtils.getInstances(MyApplication.getContext()).getSharedPreference(LoginVariable.LEVEL,0);
    }

    public static void setIsAdmin(int text){
        set(LoginVariable.IS_ADMIN,text);
    }

    public static int getIsAdmin(){
        return (int) SharedPreferencesUtils.getInstances(MyApplication.getContext()).getSharedPreference(LoginVariable.IS_ADMIN,0);
    }

    public static void setIsAuditor(int text){
        set(LoginVariable.IS_AUDITOR,text);
    }

    public static int getIsAuditor(){
        return (int) SharedPreferencesUtils.getInstances(MyApplication.getContext()).getSharedPreference(LoginVariable.IS_AUDITOR,0);
    }

    public static void setPassword(String text){
        set(LoginVariable.PASSWORD,text);
    }

    public static String getPassword(){
        return (String) SharedPreferencesUtils.getInstances(MyApplication.getContext()).getSharedPreference(LoginVariable.PASSWORD,"");
    }

    public static void setUserInfos(String text){
        set(LoginVariable.USER_INFO,text);
    }

    public static String getUserInfos(){
        return (String) SharedPreferencesUtils.getInstances(MyApplication.getContext()).getSharedPreference(LoginVariable.USER_INFO,"");
    }

    public static void setToken(String text){
        set(LoginVariable.TOKEN,text);
    }

    public static String getToken(){
        return (String) SharedPreferencesUtils.getInstances(MyApplication.getContext()).getSharedPreference(LoginVariable.TOKEN,"");
    }

    public static void setHasLogin(boolean b){
        set(LoginVariable.HAS_SUCCESS_LOGIN,b);
    }

    public static boolean hasLogin(){
        return (boolean) SharedPreferencesUtils.getInstances(MyApplication.getContext()).getSharedPreference(LoginVariable.HAS_SUCCESS_LOGIN,false);
    }

    public static void set(String key, Object text) {
        SharedPreferencesUtils.getInstances(MyApplication.getContext()).put(key,text);
    }

    public static Object get(String key,Object obj){
        return SharedPreferencesUtils.getInstances(MyApplication.getContext()).getSharedPreference(key,obj);
    }

    public static void setNeedUpdate(boolean b){
        set(AppConstant.NEED_UPDATE,b);
    }

    public static boolean getNeedUpdate(){
        return (boolean) SharedPreferencesUtils.getInstances(MyApplication.getContext()).getSharedPreference(AppConstant.NEED_UPDATE,false);
    }

    public static void reLogin(final Context context){
        try {
            AppManager.setToken("");
            MyApplication.getInstances().removeAll();
            Intent intent = new Intent();
            intent.setClass(context, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
